/*
 * @Author: Yogurt_cry 952717558@qq.com
 * @Date: 2025-08-30 00:32:15
 * @LastEditors: Yogurt_cry 952717558@qq.com
 * @LastEditTime: 2025-09-01 00:50:21
 * @Description: 
 */
/*
 * SPDX-FileCopyrightText: 2010-2022 Espressif Systems (Shanghai) CO LTD
 *
 * SPDX-License-Identifier: CC0-1.0
 */

#include <stdio.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"

#include "driver/gpio.h"

#define FAN_GPIO 4
#define FAN_ON 0
#define FAN_OFF 1

#define BUTTON_GPIO 5
#define BUTTON_PRESSED 0

#define LED_GPIO 8
#define LED_ON 0
#define LED_OFF 1

void LED_Init(void)
{
    gpio_config_t ledPinConfig;
    ledPinConfig.pin_bit_mask = (1 << LED_GPIO);
    ledPinConfig.mode = GPIO_MODE_OUTPUT;
    ledPinConfig.pull_up_en = GPIO_PULLUP_ENABLE;
    ledPinConfig.pull_down_en = GPIO_PULLDOWN_DISABLE;
    ledPinConfig.intr_type = GPIO_INTR_DISABLE;
    gpio_config(&ledPinConfig);
}

void LED_On(void)
{
    gpio_set_level(LED_GPIO, LED_ON);
}

void LED_Off(void)
{
    gpio_set_level(LED_GPIO, LED_OFF);
}

void FAN_Init(void)
{
    gpio_config_t ledPinConfig;
    ledPinConfig.pin_bit_mask = (1 << FAN_GPIO);
    ledPinConfig.mode = GPIO_MODE_OUTPUT;
    ledPinConfig.pull_up_en = GPIO_PULLUP_ENABLE;
    ledPinConfig.pull_down_en = GPIO_PULLDOWN_DISABLE;
    ledPinConfig.intr_type = GPIO_INTR_DISABLE;
    gpio_config(&ledPinConfig);
}

void FAN_On(void)
{
    gpio_set_level(FAN_GPIO, FAN_ON);
}

void FAN_Off(void)
{
    gpio_set_level(FAN_GPIO, FAN_OFF);
}

void Button_Init(void)
{
    gpio_config_t buttonPinConfig;
    buttonPinConfig.pin_bit_mask = (1 << BUTTON_GPIO);
    buttonPinConfig.mode = GPIO_MODE_INPUT;
    buttonPinConfig.pull_up_en = GPIO_PULLUP_ENABLE;
    buttonPinConfig.pull_down_en = GPIO_PULLDOWN_DISABLE;
    buttonPinConfig.intr_type = GPIO_INTR_DISABLE;
    gpio_config(&buttonPinConfig);
}

void TaskButton(void * params)
{
    int pressState = 0;
    int buttonStatus = 0;
    while (1)
    {
        if (gpio_get_level(BUTTON_GPIO) == BUTTON_PRESSED) {
            if (pressState == 0) {
                buttonStatus = (buttonStatus - 1) * -1;
                pressState++;
            }
        } else {
            pressState = 0;
        }

        if (buttonStatus == 1) {
            FAN_On();
            LED_On();
        } else {
            FAN_Off();
            LED_Off();
        }

        vTaskDelay(portTICK_PERIOD_MS);
    }
}

void TaskFAN(void * params)
{
    FAN_Init();
    while (1)
    {
        FAN_On();
        vTaskDelay(1000 / portTICK_PERIOD_MS);
        FAN_Off();
        vTaskDelay(1000 / portTICK_PERIOD_MS);
    }
}

void app_main(void)
{
    LED_Init();
    FAN_Init();
    Button_Init();
    xTaskCreate(TaskButton, "TaskButton", 2048, NULL, 1, NULL);
    // xTaskCreate(TaskFAN, "TaskFAN", 2048, NULL, 1, NULL);
}
